package org.yoki.edu.common.utils.bytes;

import java.io.UnsupportedEncodingException;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2017/11/21$ 10:05$
 */
public class ByteUtils {

    public static final int IPV4_LENGTH = 4;
    public static final int MAC_LENGTH = 6;

    /**
     * 将被"."分割的IPv4字符串切割成byte数组
     *
     * @param ipAdd
     * @return
     */
    public static byte[] ipv4ToBinaryArray(String ipAdd) {
        return ipv4ToBinaryArray(ipAdd, null);
    }

    /**
     * 分割的IPv4字符串切割成byte数组
     *
     * @param ipAdd
     * @param split 切割符，默认为"."
     * @return
     */
    public static byte[] ipv4ToBinaryArray(String ipAdd, String split) {
        byte[] binIP = new byte[IPV4_LENGTH];
        String sp = split;
        if (null == split) {
            sp = "\\.";
        }
        String[] strs = ipAdd.split(sp);
        if (strs.length > IPV4_LENGTH) {
            throw new IndexOutOfBoundsException();
        }
        for (int i = 0; i < strs.length; i++) {
            binIP[i] = (byte) Integer.parseInt(strs[i]);
        }
        return binIP;
    }

    /**
     * 将字节数组转换为IPv4字符串，默认使用"."进行分隔
     *
     * @param arr
     * @return
     */
    public static String parseToIpv4(byte[] arr) {
        if (arr.length > IPV4_LENGTH) {
            throw new IndexOutOfBoundsException();
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i] & 0xFF).append(".");
        }
        String ip = sb.substring(0, sb.length() - 1);
        return ip;
    }

    /**
     * 将字节数组转换为IPv4字符串，默认使用"."进行分隔
     *
     * @param arr
     * @param split
     * @return
     */
    public static String parseToIpv4(byte[] arr, String split) {
        if (arr.length > IPV4_LENGTH) {
            throw new IndexOutOfBoundsException();
        }
        StringBuffer sb = new StringBuffer();
        String sp = split;
        if (null == split) {
            sp = "\\.";
        }
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i] & 0xFF).append(sp);
        }
        String ip = sb.substring(0, sb.length() - 1);
        return ip;
    }

    /**
     * 将被":"分割的MAC字符串切割成byte数组
     *
     * @param mac mac字符串
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static byte[] macToBinaryArray(String mac) {
        byte[] binMac = macToBinaryArray(mac, null);
        return binMac;
    }

    /**
     * 将MAC字符串切割成byte数组
     *
     * @param mac   mac字符串
     * @param split 切割符，默认为":"
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static byte[] macToBinaryArray(String mac, String split) {
        byte[] binMac = new byte[MAC_LENGTH];
        String sp = split;
        if (null == split) {
            sp = ":";
        }
        String[] strs = mac.split(sp);
        if (strs.length > MAC_LENGTH) {
            throw new IndexOutOfBoundsException();
        }
        for (int i = 0; i < strs.length; i++) {
            binMac[i] = (byte) Integer.parseInt(strs[i]);
        }
        return binMac;
    }

    /**
     * 将被":"分割的16进制组成的MAC字符串切割成byte数组
     *
     * @param mac mac字符串
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static byte[] hexMacToBinaryArray(String mac) throws IndexOutOfBoundsException {
        byte[] binMac = hexMacToBinaryArray(mac, null);
        return binMac;
    }

    /**
     * 将16进制组成的MAC字符串切割成byte数组
     *
     * @param mac   mac字符串
     * @param split 切割符，默认为":"
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static byte[] hexMacToBinaryArray(String mac, String split) throws IndexOutOfBoundsException {
        byte[] binMac = new byte[MAC_LENGTH];
        String sp = split;
        if (null == split) {
            sp = ":";
        }
        String[] strs = mac.split(sp);
        if (strs.length > MAC_LENGTH) {
            throw new IndexOutOfBoundsException();
        }
        for (int i = 0; i < strs.length; i++) {
            binMac[i] = (byte) Integer.parseInt(strs[i], 16);
        }
        return binMac;
    }

    /**
     * 将字节数组转换为MAC字符串，默认使用":"进行分隔
     *
     * @param arr 字节数组
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static String parseToMac(byte[] arr) throws IndexOutOfBoundsException {
        return parseToMac(arr, null);
    }

    /**
     * 将字节数组转换为MAC字符串
     *
     * @param arr   字节数组
     * @param split 分隔符
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static String parseToMac(byte[] arr, String split) throws IndexOutOfBoundsException {
        if (arr.length > MAC_LENGTH) {
            throw new IndexOutOfBoundsException();
        }
        StringBuffer sb = new StringBuffer();
        String sp = split;
        if (null == split) {
            sp = ":";
        }
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i] & 0xFF).append(sp);
        }
        String mac = sb.substring(0, sb.length() - 1);
        return mac;
    }

    /**
     * 将byte数组转成16进制组成的MAC字符串，默认使用":"进行分隔
     *
     * @param arr byte数组
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static String parseToHexMac(byte[] arr) throws IndexOutOfBoundsException {
        return parseToHexMac(arr, null);
    }

    /**
     * 将byte数组转成16进制组成的MAC字符串
     *
     * @param arr   byte数组
     * @param split 分隔符号，默认使用":"
     * @return
     * @throws IndexOutOfBoundsException
     */
    public static String parseToHexMac(byte[] arr, String split) throws IndexOutOfBoundsException {
        if (arr.length > MAC_LENGTH) {
            throw new IndexOutOfBoundsException();
        }
        StringBuffer sb = new StringBuffer();
        String sp = split;
        if (null == split) {
            sp = ":";
        }
        for (int i = 0; i < arr.length; i++) {
            sb.append(Integer.toHexString((arr[i] >> 4) & 0xF)).append(Integer.toHexString(arr[i] & 0xF)).append(sp);
        }
        String mac = sb.substring(0, sb.length() - 1);
        return mac;
    }

    /**
     * 根据byte数组获取UTF-8编码的字符串
     *
     * @param input
     * @return
     */
    public static String getUtf8String(byte[] input) {
        String str;
        try {
            str = new String(input, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            str = new String(input);
        }
        return str;
    }

    /**
     * 根据字符串获取UTF-8编码的数组
     *
     * @param input
     * @return
     */
    public static byte[] getUtf8Byte(String input) {
        byte[] bytes;
        try {
            bytes = input.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            bytes = input.getBytes();
        }
        return bytes;
    }

}
